﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace NEWSPAPERINFOMATION
{
    class BookConnection
    {
        SqlConnection con;

        public void open1()
        {
            if (con == null)
            {
                string strConn = "data source=localhost;initial catalog=BOOKING;user=sa;pwd=123456;Persist Security Info=True";
                con = new SqlConnection(strConn);
            }
            else if (con.State == System.Data.ConnectionState.Closed)
            {
                con.Open();
            }
        }
        private void close()//数据库关闭
        {
            if (con != null)
            {
                con.Close();
            }
        }
        //增加、修改、删除操作，返回结果
        public int executeSqlResult(string sqlStr)//返回查询行数
        {
            open1();
            if (con.State == System.Data.ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            int t = cmd.ExecuteNonQuery();
            close();
            return t;
        }
        public int runSqlInt(string sqlStr)//返回查询行数
        {
            open1();
            if (con.State == System.Data.ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd); //创建DataAdapter数据适配器实例  
            DataTable dt = new DataTable();
            da.Fill(dt);
            int t = dt.Rows.Count;
            close();
            return t;
        }
        //执行查询，返回datatable
        public DataTable runSql(string sqlStr)
        {
            open1();
            SqlCommand cmd = new SqlCommand(sqlStr, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd); //创建DataAdapter数据适配器实例  
            DataTable dt = new DataTable();
            da.Fill(dt);
            close();
            return dt;
        }
    }
}